package graphic;

import java.util.logging.Level;
import java.util.logging.Logger;
import klient.Klient;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

/**
 *  
 * @author Marcin & Bartek
 * 
 * @description initialize our display of the screen. Two options: fullscreen/normal
 */
public final class DisplayInit {
    private DisplayInit(){}
    public static boolean SetResolution(int width, int height, boolean fullscreen){
        Klient.log("???");
        try{
        if ((Display.getDisplayMode().getWidth() == width) && 
                (Display.getDisplayMode().getHeight() == height)&&
                (Display.isFullscreen() == fullscreen)){
            Klient.log("X_x"); 
            return true;
        }
        }catch(Exception e){
            Klient.log("O_o");     
            e.printStackTrace();
        }
        DisplayMode target=null;
        Klient.log("!!!");        
        if(fullscreen)
            target = tryFullscreen(width,height);            
        if (target == null){
            fullscreen=false;
            target = new DisplayMode(width, height);
        }
        if (target == null)    
            Klient.log("Błąd inicjalizacji grafiki!");
        else try{       
            Display.setDisplayMode(target);
            Display.setFullscreen(fullscreen);
            Klient.log("Zainicjalizowano! "+target.getWidth()+"x"+target.getHeight()+" "+target.getBitsPerPixel()+"BPP "+
                    target.getFrequency()+"Hz "+(fullscreen?"fullscreen":""));
            return true;
        }catch (Exception e){
            Klient.log("Inicjalizacja nieudana!");
        }
        return false;
    }
    
    public static DisplayMode tryFullscreen(int width, int height){
        DisplayMode target=null;
        DisplayMode[] modes;
        try {
            modes = Display.getAvailableDisplayModes();
        } catch (LWJGLException ex) {
            Klient.log("Błąd inicjalizacji pełnego ekranu!");
            return null;
        }
        int freq = 0;
            
        for(int i=0;i<modes.length; i++){
            DisplayMode current = modes[i];
            if ((current.getWidth() == width) && (current.getHeight() == height)){
                if ((target == null)||(current.getFrequency() >= freq)){
                    if ((target == null) || (current.getBitsPerPixel() > target.getBitsPerPixel())){
                        target = current;
                        freq = target.getFrequency();
                    }
                }
                if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
                        (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
                    target = current;
                    break;
                }                    
            }
        }
        if(freq==0) return null;
        return target;
    }
}
